/*
 * This class will upload comment for meal by user
 */
package com.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.configuration.Configuration;
import com.configuration.Owner;
import com.object.MealComment;
import com.user.JSONParser;

public class MealCommentUpdate {
	//URL for fetching data
	private static String uploadMealCommentURL		= "http://128.199.214.245/android/MealCommentUpdate/upload_meal_comment.php";
	private static String removeMealCommentURL		= "http://128.199.214.245/android/MealCommentUpdate/remove_meal_comment.php";
	private static String likeMealURL				= "http://128.199.214.245/android/MealCommentUpdate/like_meal.php";
	private static String unlikeMealURL				= "http://128.199.214.245/android/MealCommentUpdate/unlike_meal.php";

	//Keys for the Json field
	/*
	 * KEY_STATUS shows the status of the request
	 * 0 : Fail to get restaurant
	 * 1 : Successfully in getting restaurant
	 */
	private String KEY_ID				= "ID";
	private String KEY_USER				= "USER";
	private String KEY_MEAL				= "MEAL";
	private String KEY_COMMENT			= "COMMENT";
	private String KEY_CREATE_DATE		= "CREATE_DATE";
	    	    
	private String KEY_STATUS 			= "STATUS";
	private String KEY_NUMBER 			= "NUMBER";		//Returned by server telling how many restaurants 
	    
	private String FAIL 	= "0";
	private String SUCCESS 	= "1";
	    
	//For making httpRequest
	private JSONParser jsonParser;
	private JSONObject result;
	List<NameValuePair> params;
	
	//Constructor
	public MealCommentUpdate(){
	    jsonParser = new JSONParser();
	}
		
	//Upload a new comment
	public int uploadMealComment(int user,int meal,String comment){
		//Get Json object
		// Building Parameters
	    params = new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("user",Integer.toString(user)));
	    params.add(new BasicNameValuePair("meal",Integer.toString(meal)));
	    params.add(new BasicNameValuePair("comment",comment));
	       
	       
	       //Start a new thread and use POST method to send telephone and passcode to authURL
	       //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(uploadMealCommentURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		//Parse the json object
		try {
			if(result.getString(KEY_STATUS) != null){
				//Able to get response from server
				if(result.getString(KEY_STATUS).equalsIgnoreCase(SUCCESS)){
					//Successful in finding the user
					if(Configuration.showLog){
						Log.d("MealCommentUpdate","uploadMealComment - upload ok");
					}
					
					return Integer.parseInt(SUCCESS);
					
				}else{
					//Fail to find such user
					if(Configuration.showLog){
						Log.d("MealCommentUpdate","uploadMealComment - upload");
					}
				}
			}else{
				//unable to get response from server
				if(Configuration.showLog){
					Log.d("MealCommentUpdate","uploadMealComment - Unable to get response from server");
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			if(Configuration.showLog){
				Log.d("MealCommentUpdate","uploadMealComment - Exception");
			}
		}
		return Integer.parseInt(FAIL);
	}
	
	/*
	 * This function remove meal comment by meal id
	 */
	public void removeMealComment(int mealCommentId){
		//Get Json object
		// Building Parameters
	    params = new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("id",Integer.toString(mealCommentId)));
	       
	       
	       //Start a new thread and use POST method to send telephone and passcode to authURL
	       //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(removeMealCommentURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
	}
	
	public void likeMeal(int mealId){
		//Get Json object
		// Building Parameters
	    params = new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("meal_id",Integer.toString(mealId)));
		params.add(new BasicNameValuePair("user_id",Integer.toString(Owner.id)));
	       
	       //Start a new thread and use POST method to send telephone and passcode to authURL
	       //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(likeMealURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		Log.d("MealCommentUpdate","Done LIKE");
	}
	
	public void unlikeMeal(int mealId){
		//Get Json object
		// Building Parameters
	    params = new ArrayList<NameValuePair>();
	    params.add(new BasicNameValuePair("meal_id",Integer.toString(mealId)));
		params.add(new BasicNameValuePair("user_id",Integer.toString(Owner.id)));
	       
	       //Start a new thread and use POST method to send telephone and passcode to authURL
	       //Start a new thread to attempt Login (requirement when connecting online)
		Thread attemptThread = new Thread(new Runnable(){  
			public void run(){  
				result = jsonParser.makeHttpRequest(unlikeMealURL,"POST",params);
			}
		});  
		attemptThread.start();
		
		while(attemptThread.isAlive()){
			//Wait for server response
		}
		
		Log.d("MealCommentUpdate","Done UNLIKE");
	}
}
